package controller;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import model.interactionhandler.InteractionContainer;
import view.GameWindow;
import view.OverlayIds;
import view.TalkOverlay;
import view.View;

public class TalkController extends Controller {

	
	private GameWindow gw;
	private TalkOverlay to;
	InteractionContainer ic;
	
	public TalkController( GameWindow gw, TalkOverlay to, InteractionContainer ic) {
		this.gw = gw;
		this.to = to;
		this.ic = ic;
		to.register( this );
		this.initiate();
	}
	
	private void initiate() {
		this.gw.addKeyListener( new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {}
			@Override
			public void keyReleased(KeyEvent arg0) {
				if( arg0.getKeyCode() == GameConfig.EXIT_INTERACTION ) {
					doneLogic();
				}
			}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		} );
		this.gw.addKeyListener( new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {}
			@Override
			public void keyReleased(KeyEvent arg0) {
				if( arg0.getKeyCode() == GameConfig.NEXT_DIALOGUE ) {
					if( ! to.isDone() ) {
						to.advanceToNextLine();
					}
				}
			}
			@Override
			public void keyTyped(KeyEvent arg0) {}
		} );
	}
	
	@Override
	public void update(View v) {
		if( to.isDone() ) {
			doneLogic();
		} 
	}

	private void doneLogic() {
		ic.clear();
		gw.getOverlay( OverlayIds.TALK ).makeInvisible();
	}
	
}
